/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.graph;

/**
 *
 * @author Bayu
 */
import com.bayu.common.lib.CommonUtil;
import com.bayu.controller.MainController;
import com.bayu.services.dao.TRejectionMouldSm60Dao;
import com.bayu.services.entity.TRejectionMouldSm60;
import java.awt.Color;
import java.io.OutputStream;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RejectionMouldSm60Graph {
    private TRejectionMouldSm60Dao dao;

    @Autowired
    public void setDao(TRejectionMouldSm60Dao dao) {
        this.dao = dao;
    }

    @RequestMapping("/mould-sm60-graph.png")
    public void drawChart(OutputStream stream) throws Exception {
        JFreeChart chart = getChart();
        ChartUtilities.writeChartAsPNG(stream, chart, 1480, 800);
    }

    private JFreeChart getChart() {
        String title="Rejection Mould: SM60";
        String tgl1, tgl2;
        if(!MainController.startDate.equals("")){
            tgl1 = CommonUtil.formatStringDate(MainController.startDate);
            tgl2 = CommonUtil.formatStringDate(MainController.endDate);
            title = title+"\r\n"+ tgl1+"-"+tgl2;
        }

        DefaultCategoryDataset dataset = getDataset();
        final JFreeChart chart = ChartFactory.createLineChart(
            title,
            "Date",
            "% reject",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );
        chart.setBackgroundPaint(new Color(204, 255, 204));
        CategoryPlot plot = chart.getCategoryPlot();

        plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

        CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.green);
        renderer.setBaseNegativeItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        renderer.setBaseItemLabelsVisible(true);

        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setAutoRange(true);
//        rangeAxis.setRange(0, max);
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        return chart;
    }

    private DefaultCategoryDataset getDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        List<TRejectionMouldSm60> data = this.dao.getAllByDate(MainController.startDate, MainController.endDate);
        for (TRejectionMouldSm60 object : data) {
            String date[] = object.getDate().toString().split("-");
            dataset.addValue(object.getHarian(), "% day", date[2]);
            dataset.addValue(object.getKum(), "% cumm", date[2]);
        }
        return dataset;
    }
}
